<!--
 * @Author: cqg
 * @Date: 2020-12-10 15:16:08
 * @LastEditTime: 2020-12-22 17:03:42
 * @Description: file content
 * @LastEditors: cqg
-->

<template>
  <div class="card-box">
    <el-dialog :title="title" :visible.sync="formVisible" width="1200px" @close="close">
      <el-form :model="form" class="form-wrap" ref="form" :rules="rules">
        <p class="title full">工程信息</p>
        <el-form-item label="工程名称" prop="projectName">
          <el-input v-model="form.projectName" :disabled="type=='preview'" suffix-icon="el-icon-search" @focus="$refs.chooseDialog.show()" />
        </el-form-item>
        <el-form-item label="工程地址" prop="projectAddress">
          <el-input disabled v-model="form.projectAddress" />
        </el-form-item>
        <el-form-item label="工程类型" prop="projectType">
          <el-select disabled v-model="form.projectType" placeholder="-=请选择=-" filterable>
            <el-option v-for="item in epcProjectTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <p class="title full">财务决算</p>
        <el-form-item label="文号" prop="finalDocument">
          <el-input :disabled="type=='preview'" v-model="form.finalDocument" maxlength="20" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="金额(元)" prop="finalAmount">
          <el-input :disabled="type=='preview'" placeholder="请输入" maxlength="16" v-model.number="form.finalAmount" v-only-number="{min:0,precision:2}" />
        </el-form-item>
        <el-form-item label="决算日期" prop="finalDate">
          <el-date-picker :disabled="type=='preview'" v-model="form.finalDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" />
        </el-form-item>
        <p class="title full">附件</p>
      </el-form>
      <v-upload
        v-if="formVisible"
        ref="upload"
        :busId="busId"
        modelType="finalCost"
        @loadCompleted="handleLoadCompleted"
        @onSuccess="onSuccess"
        v-model="uploadFileList"
        :isView="['preview'].includes(type)"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" v-throttle="[save]" v-if="['add','edit'].includes(type)">保存</el-button>
      </div>
    </el-dialog>
    <choose-dialog ref="chooseDialog" v-bind="chooseDialogAttrs" @chooseData="handleChoose" />
  </div>
</template>
<script>
import {
  updateOrSave,
  validateCode
} from "./api.js";
import { getDict } from "@/utils";
export default {
  name: "finalCost",
  inject: ["initList"],
  data () {
    return {
      // 已上传的文件
      oldFiles: [],
      //上传id
      busId: "",
      // 上传列表
      uploadFileList: [],
      //弹框title
      title: "",
      //操作类型
      type: "",
      //验收弹窗开关
      formVisible: false,
      // 表单数据
      form: {
        projectId: "",//工程id
        projectFinalId: "",//当前模块主键id
        projectEndId: "",//上级流程id
        projectName: "",
        projectType: "",
        projectAddress: "",
        finalDocument: "", //文号
        finalDate: "", //决算日期
        finalAmount: "", //金额
      },
      //表单验证规则
      rules: {
        projectName: [
          {
            required: true,
            message: "请填选完整工程信息",
            trigger: "change"
          }
        ],
        finalDocument: [
          {
            required: true,
            message: "请填结算文号",
            trigger: "change"
          },
          { validator: this.validateSettle, trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    // 工程类型
    epcProjectTypes () {
      return getDict("epc_project_type");
    },
    //弹框勾选列表配置 
    chooseDialogAttrs () {
      return {
        title: "选择工程",
        placeholder: "请输入工程名称",
        url: "/api/epcprojectfinal/selectProject",
        primaryKey: "projectId",
        width: "1000px",
        searchKey: "projectName",
        showIndex: true,
        showData: [
          {
            prop: "projectName",
            label: "工程名称",
            align: "center"
          },
          {
            prop: "projectAddress",
            label: "工程地址",
            align: "center"
          }
        ]
      }
    }
  },
  methods: {
    // 监听上传组件上传完成
    handleLoadCompleted (data) {
      this.oldFiles = data
    },
    //   结算 唯一校验
    async validateSettle (rule, value, callback) {
      let rps = await validateCode({
        finalDocument: value,
        projectFinalId: this.form.projectFinalId
      });
      if (rps.data.data == "1" && rps.data.code == '0') callback(new Error("该决算文号已存在"));
      callback();
    },
    // 关闭弹窗清空发表单
    close () {
      this.uploadFileList.length = 0;
      this.oldFiles.length = 0
      for (var i in this.form) {
        this.form[i] = Object.prototype.toString.call(this.form[i]) == "[object Array]" ? [] : "";
      }
      this.$refs.form.clearValidate();
      this.formVisible = false;
    },
    // 监听弹窗选中
    handleChoose (data) {
      let sData = {
        projectId: data.projectId,
        projectName: data.projectName,
        projectAddress: data.projectAddress,
        projectType: data.projectType
      }
      this.form = { ...this.form, ...sData };
    },
    //显示弹窗
    show (type, data = {}) {

      // 判断类型渲染表单标题
      switch (type) {
        case "add":
          this.title = "新增";
          break;
        case "edit":
          this.title = "编辑";
          break;
        case "preview":
          this.title = "查看";
          break;
      }
      this.type = type;
      this.formVisible = true;
      this.form = { ...this.form, ...data };
      if (data) this.busId = this.form.projectFinalId;
    },
    // 保存
    save () {
      if (["edit", "add"].includes(this.type)) {
        this.$refs.form.validate(valid => {
          valid && this.$refs.upload.submit();
        });
      }
    },
    // 上传成功
    async onSuccess (response, file, fileList) {
      let params = this.type == "add" ? { ...this.form, projectFinalId: response.data } : JSON.parse(JSON.stringify(this.form));
      const rps = await updateOrSave(this.type, params);
      if (rps.data.code == 0) {
        this.$message.success(`提交成功!`);
        this.formVisible = false;
        this.initList({ projectName: "", current: 1 });
      } else this.$message.error(rps.data.msg);
    }
  }
};
</script>
<style lang="scss" scoped>
@include formWrap(4);
.title {
  border-bottom: 1px solid rgba(97, 106, 120, 0.07);
  height: 50px;
  margin-left: -25px;
  line-height: 50px;
  font-weight: 700;
  font-size: 16px;
}
</style>
